
module.exports = class extends baseController{
    constructor(){
        super();
        this.orgStruct=this.loadLogic('iframe/org-struct');
        this.router.post('/getItems',this.getItems.bind(this));
        this.router.post('/edit',this.edit.bind(this));
        this.router.post('/del',this.del.bind(this));
        this.router.post('/move',this.move.bind(this));
        this.router.post('/syncOrg',this.syncOrg.bind(this));
        this.router.post('/getItemsByPartnersId',this.getItemsByPartnersId.bind(this));
        this.router.post('/getPartnersTreeData',this.getPartnersTreeData.bind(this));
        return this.router;
    }

    static init(){
        return new this();
    }

    async getItems(req,res){
        let Result={};
        try{
           // let partnersID=req.body.PartnersID? Number(req.body.PartnersID) : Number(req.user.UserPartnersID);
            let OrgItems=await this.orgStruct.getItems(req.headers.moduleid,req.user);

            Result=this.Success(1,OrgItems);
        }catch(err){
            Result=this.Error("读取数据出错",err);
        }
        res.json(Result);
    }

    async edit(req,res){
        let Result={};
        try{
            let Model = await this.orgStruct.insertOrUpdate(req.body,req.user);
            Result=this.Success(1,Model);
        }catch(err){
            Result=this.Error("读取数据出错",err);
        }
        res.json(Result);
    }

    async del(req,res){
        let Result={};
        try{
            await this.orgStruct.del(req.body);
            Result=this.Success(1);
        }catch(err){
            Result=this.Error("读取数据出错",err);
        }
        res.json(Result);
    }

    async move(req,res){
        let Result={};
        try{
            await this.orgStruct.move(req.body);
            Result=this.Success(1);
        }catch(err){
            Result=this.Error("读取数据出错",err);
        }
        res.json(Result);
    }

    async syncOrg(req,res){
        let Result={};
        try{
             let ret =await  this.orgStruct.syncOrg();
             if(ret){
                Result=this.Success(1);
             }else{
                Result=this.Error('数据同步失败');
             }

        }catch(err){
            Result=this.Error("读取数据出错",err);
        }
        res.json(Result);
    }

    async getItemsByPartnersId(req,res){
        let Result={};
        try{
            let queryModel={
                partnersId:Number("0" + req.body.partnersId),
                parentId:req.body.parentId || ''
            };
             let ret =await  this.orgStruct.getItemsByPartnersId(queryModel,req.user);
             if(ret){
                Result=this.Success(1,ret);
             }else{
                Result=this.Error('数据同步失败');
             }

        }catch(err){
            Result=this.Error("读取数据出错",err);
        }
        res.json(Result);
    }

    /**
     * 获取企业列表的树形结构
     * @param req
     * @param res
     * @returns {Promise<void>}
     */
    async getPartnersTreeData(req,res){
        let Result={};
        try{
            let ret = await this.orgStruct.getPartnersTreeData(req.headers.moduleid,req.user);
            Result=this.Success(1,ret);

        }catch (err) {
            Result=this.Error("读取数据出错",err);
        }
        res.json(Result);
    }
};
